#import <objc/runtime.h>
#import "UIView+PPPExt.h"
#import "dfhgwhruebvhrheychemistcandidateViewController.h"
#import <MBProgressHUD/MBProgressHUD.h>
@interface UIView ()<MBProgressHUDDelegate>
@property (nonatomic, assign) BOOL loading;
@end
@implementation UIView (PPPExt)
- (CGRect)php_rect {
	return self.frame;
}
- (CGFloat)php_x {
	return [self php_rect].origin.x;
}
- (void)setPhp_x:(CGFloat)php_x {
	CGRect rect = [self php_rect];
	rect.origin.x = php_x;
	self.frame = rect;
}
- (CGFloat)php_y {
	return [self php_rect].origin.y;
}
- (void)setPhp_y:(CGFloat)php_y {
	CGRect rect = [self php_rect];
	rect.origin.y = php_y;
	self.frame = rect;
}
- (CGFloat)php_w {
	return [self php_rect].size.width;
}
- (void)setPhp_w:(CGFloat)php_w {
	CGRect rect = [self php_rect];
	rect.size.width = php_w;
	self.frame = rect;
}
- (CGFloat)php_h {
	return [self php_rect].size.height;
}
- (void)setPhp_h:(CGFloat)php_h {
	CGRect rect = [self php_rect];
	rect.size.height = php_h;
	self.frame = rect;
}
- (CGFloat)php_center_y {
	return self.center.y;
}
- (void)setPhp_center_y:(CGFloat)php_center_y {
	self.center = CGPointMake(self.center.x, php_center_y);
}
- (CGFloat)php_center_x {
	return self.center.x;
}
- (void)setPhp_center_x:(CGFloat)php_center_x {
	self.center = CGPointMake(self.center.x, php_center_x);
}
- (dfhgwhruebvhrheychemistcandidateViewController *)controller {
	UIResponder *responder = [self nextResponder];
	while (![responder isKindOfClass:[dfhgwhruebvhrheychemistcandidateViewController class]] && responder != nil) {
		responder = responder.nextResponder;
	}
	return (dfhgwhruebvhrheychemistcandidateViewController *)responder;
}
#pragma mark: ============ 绘制相关 =========
-(void)drawBoardWithCornerRadius:(CGFloat)cornerRadius andColor:(UIColor*)color {
	self.layer.cornerRadius = cornerRadius;
	CAShapeLayer *borderLayer = [CAShapeLayer layer];
	borderLayer.bounds = self.bounds;
	borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
	borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:cornerRadius].CGPath;
	borderLayer.lineWidth = 1 / [[UIScreen mainScreen] scale];
	borderLayer.lineDashPattern = @[@(5), @(5)];
	borderLayer.lineDashPhase = 0.1;
	borderLayer.fillColor = [UIColor clearColor].CGColor;
	borderLayer.strokeColor = color.CGColor;
	[self.layer insertSublayer:borderLayer atIndex:0];
}
- (void)drawDashLine:(UIColor *)color {
	UIBezierPath *path = [UIBezierPath bezierPath];
	[path moveToPoint:CGPointMake(0, 0)];
	[path addLineToPoint:CGPointMake(self.bounds.size.width, 0)];
	CAShapeLayer *lineLayer = [CAShapeLayer layer];
	lineLayer.path = path.CGPath;
	lineLayer.lineWidth = 1;
	lineLayer.lineDashPattern = @[@(5), @(5)];
	lineLayer.strokeColor = color.CGColor;
	[self.layer addSublayer:lineLayer];
}
- (void)addUnderLine:(CGFloat)lineWidth andColor:(UIColor *)color {
    [self layoutIfNeeded];
    UIBezierPath *bezier = [UIBezierPath bezierPath];
    [bezier moveToPoint:CGPointMake(0, CGRectGetHeight(self.bounds))];
    [bezier addLineToPoint:CGPointMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
    for (CALayer *layer in self.layer.sublayers) {
        if ([layer isKindOfClass:[CAShapeLayer class]]) {
            [layer removeFromSuperlayer];
        }
    }
    CAShapeLayer *shape = [CAShapeLayer layer];
    shape.path = bezier.CGPath;
    shape.lineWidth = lineWidth;
    shape.strokeColor = color.CGColor;
    [self.layer addSublayer:shape];
}
#pragma mark:  ========== toast相关 =============
- (BOOL)loading {
	return objc_getAssociatedObject(self, _cmd);
}
- (void)setLoading:(BOOL)loading {
	objc_setAssociatedObject(self, @selector(isLoading), @(loading), OBJC_ASSOCIATION_ASSIGN);
}
- (MBProgressHUD *)hud {
	return objc_getAssociatedObject(self, _cmd);
}
- (void)setHud:(MBProgressHUD *)hud {
	objc_setAssociatedObject(self, @selector(hud), hud, OBJC_ASSOCIATION_ASSIGN);
}
- (void)startLoading {
    if (self.hud) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    self.loading = YES;
    hud.delegate = self;
    self.hud = hud;
}
- (void)stopLoading {
	if([[NSThread currentThread] isMainThread]) {
		[self.hud hideAnimated:YES];
	}else {
		dispatch_async(dispatch_get_main_queue(), ^{
			[self.hud hideAnimated:YES];
		});
	}
	self.hud = nil;
}
- (void)show:(NSString *)text {
	if (text.length == 0) {
		return;
	}
	MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
	hud.tintColor = [UIColor whiteColor];
	hud.contentColor = [UIColor whiteColor];
	hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
	hud.bezelView.backgroundColor = [UIColor blackColor];
	hud.userInteractionEnabled = NO;
	hud.mode = MBProgressHUDModeText;
	hud.label.text = text;
	hud.label.numberOfLines = 0;
    hud.offset = CGPointMake(0.f, PPP_SCREEN_HEIGHT/6);
	[hud hideAnimated:YES afterDelay:3.f];
}
#pragma mark : ========= MBProgressHUD Delegate
- (void)hudWasHidden:(MBProgressHUD *)hud {
	self.loading = NO;
}
@end
